package com.android.personal.edit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.personal.data.FinanceDataTags;
import com.android.personal.finance.R;

public class AccountsEditActivity extends Activity {
	
	private String id;
	private String name;
	private String balance;
	
	private EditText titleEdit;
	private EditText balanceEdit;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_edit);
		
		Button confirmButton = (Button) findViewById(R.id.confirm);
		
    	titleEdit = (EditText) findViewById(R.id.account_title);
		balanceEdit = (EditText) findViewById(R.id.account_balance);
		
		Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	
            id = extras.getString(FinanceDataTags.ACCOUNT_KEY);
            name = extras.getString(FinanceDataTags.ACCOUNT_NAME);
            balance = extras.getString(FinanceDataTags.ACCOUNT_BALANCE);
            
            if (name != null) {
            	titleEdit.setText(name);
            }
            if (balance != null) {
            	balanceEdit.setText(balance);
            }
        }
        
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	
                Bundle bundle = new Bundle();
                
                bundle.putString(FinanceDataTags.ACCOUNT_NAME, 
                		titleEdit.getText().toString());
                bundle.putString(FinanceDataTags.ACCOUNT_BALANCE, 
                		balanceEdit.getText().toString());
                if (id != null) {
                    bundle.putString(FinanceDataTags.ACCOUNT_KEY, id);
                }
                
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
            
        });
	}
}
